/**
 * <p>Title: IPostDao.java</p>
 * <p>Description: DoNotes</p>
 * @author WangJun
 * @date 2009/11/15 19:44:00
 * @version 1.0
 */
package net.donotes.blog.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import net.donotes.blog.pojo.Options;
import net.donotes.blog.pojo.Post;
import net.donotes.util.dao.PMF;

/**
 * @author WangJun
 *
 */
public class PostDao {
	/**
	 * Title: addPost
	 * @param post
	 */
	public void addPost(Post post) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
        try {
        	post.setCreateDate(new Date());
        	post.setUpdateDate(new Date());
            pm.makePersistent(post);
        } finally {
            pm.close();
        }
	}
	
	/**
	 * Title: delPost
	 * @param post
	 */
	public void delPost(Post post) {
		// TODO Auto-generated method stub

	}
	
	/**
	 * 
	 * Title: getPost
	 * @param id
	 */
	public Post getPost(Long id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Post.class);
	    query.setFilter("id == idParam");
		query.declareParameters("Long idParam");

		Post post = new Post();
		List<Post> resultes = (List<Post>)query.execute(id);
		if (resultes.iterator().hasNext()) {
			post = resultes.get(0);
		} else {
			post = null;
		}
		pm.close();
		return post;
	}
	
	/**
	 * Title: updPost
	 * @param post
	 */
	public void updPost(Post post) {
	    PersistenceManager pm = PMF.get().getPersistenceManager();
	    try {
	    	Post t = pm.getObjectById(Post.class, post.getId());
	    	t.setAuthor(post.getAuthor());
	    	t.setContent(post.getContent());
	    	t.setCategory(post.getCategory());
	    	//t.setCommentCounter(post.getCommentCounter());
	    	//t.setCreateDate(new Date());
	    	//t.setFrequency(post.getFrequency());
	    	//t.setStatus(post.getStatus());
	    	t.setTags(post.getTags());
	    	t.setTitle(post.getTitle());
	    	//t.setType(post.getType());
	    	//t.setUpdateDate(new Date());
	    	
	    	pm.makePersistent(post);
	    } finally {
	        pm.close();
	    }
	}
	
	/**
	 * Title: listPost
	 * @return
	 */
	public List<Post> listPost() {
		List<Post> posts = new ArrayList<Post>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Post.class);
		query.setRange(0, 10);
		query.setOrdering("createDate desc");
	    try {
	    	posts = (List<Post>) query.execute();
/*    	
	    	if (posts.iterator().hasNext()) {	    		
	            for (Post e : posts) {
	            	pm.deletePersistent(e);	
	            }	    		
	    	}
*/	    		    	
	    } finally {
	        query.closeAll();
	        //pm.close();
	    }
	    
		return posts;
	}
	
	/**
	 * Title: listPost
	 * @return
	 */
	public List<Post> listPost(long rangeFrom, long rangeTo, String category) {
		List<Post> posts = new ArrayList<Post>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Post.class);

		if (category != null && category != "" ) {
		    query.setFilter("category == categoryParam");
			query.declareParameters("String categoryParam");
		}
		query.setRange(rangeFrom, rangeTo);
		query.setOrdering("createDate desc");
	    try {
			if (category != null && category != "" ) {
				posts = (List<Post>) query.execute(category);
			} else {
				posts = (List<Post>) query.execute();
			}
	    } finally {
	        query.closeAll();
	        //pm.close();
	    }
		return posts;
	}
	
	/**
	 * Title: getPostsCounter
	 * @return
	 */
	public int getPostsCounter(String category) {
		List<Post> posts = null;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Post.class);

		int postsCounter = 0;
		if (category != null && category != "" ) {
		    query.setFilter("category == categoryParam");
			query.declareParameters("String categoryParam");
		}
	    try {
			if (category != null && category != "" ) {
				posts = (List<Post>) query.execute(category);
			} else {
				posts = (List<Post>) query.execute();
			}
			postsCounter = posts.size();
	    } finally {
	        query.closeAll();
	    }
		return postsCounter;
	}

	public List<Post> exeQuery(String query, Map parameters) {
		List<Post> posts = new ArrayList<Post>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		return (List<Post>)pm.newQuery(query).executeWithMap(parameters);
		// pm.close();
	}
}